﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CarExam.DAL
{
    public class ItemBankDAL
    {
        /// <summary>
        /// 获取题库数据
        /// </summary>
        /// <param name="type">1:顺序考试 2:随机考试</param>
        /// <param name="startindex">如果是顺序考试，则此参数为顺序取题的起始位置；
        /// 如果是随机考试，则此参数为随机取题时对应的题库表中的主键id
        /// </param>
        public DataTable GetItemBank(int type, int startindex)
        {
            string sql = "";
            switch (type)
            {
                case 1:
                    sql = "select top 100 * from ItemBank where tid>=@tid order by tid";
                    break;
                case 2:
                    sql = "select * from ItemBank where tid=@tid";
                    break;
            }
            SqlParameter[] param = {
                                       new SqlParameter("@tid",startindex)
                                   };
            DataSet ds = DBUtility.DbHelperSQL.Query(sql, param);
            if (ds != null && ds.Tables.Count > 0)
                return ds.Tables[0];
            return null;
        }

        public DataTable GeterrorRate(int tid)
        {
            string sql = "select * from [ErrorItem] where tid=@tid";
            SqlParameter[] param = {
                                       new SqlParameter("@tid",tid)
                                   };
            DataSet ds = DBUtility.DbHelperSQL.Query(sql, param);
            if (ds != null && ds.Tables.Count > 0)
                return ds.Tables[0];
            return null;
        }

        /// <summary>
        /// 添加错题到错题本中
        /// </summary>
        /// <param name="uid">当前考生id</param>
        /// <param name="tid">题号id</param>
        public bool InsertErrorItem(int uid, int tid, int isok)
        {
            SqlParameter[] param = {
                                       new SqlParameter("@uid",uid),
                                       new SqlParameter("@tid",tid),
                                       new SqlParameter("@isok",isok)
                                   };
            int res = 0;
            DBUtility.DbHelperSQL.RunProcedure("Spm_InsertErrorItem", param, out res);
            return res > 0 ? true : false;
        }

        /// <summary>
        /// 获取错题本中的所有数据
        /// </summary>
        /// <param name="uid">当前考试的id</param>
        /// <param name="type">0:错题练习 1:模拟考试</param>
        public DataTable GetErrorItem(int uid, int type = 0)
        {
            string sql = "";
            if (type == 0)
                sql = "select ei.*,ib.* from ErrorItem ei inner join ItemBank ib on ei.tid=ib.tid where ei.uid=@uid and ei.errorrate!='0' order by ei.eid";
            else if (type == 1)
                sql = "select top 20 ib.* from ErrorItem ei inner join ItemBank ib on ei.tid=ib.tid where ei.uid=@uid and ei.errorrate!='0' order by ei.errorrate desc";
            SqlParameter[] param = {
                                       new SqlParameter("@uid",uid)
                                   };
            DataSet ds = DBUtility.DbHelperSQL.Query(sql, param);
            if (ds != null && ds.Tables.Count > 0)
                return ds.Tables[0];
            return null;
        }

        /// <summary>
        /// 获取题库中的总记录数
        /// </summary>
        public int GetItemBankCount()
        {
            int total = 0;
            string sql = "select count(*)as Record from ItemBank";
            DataSet ds = DBUtility.DbHelperSQL.Query(sql);
            if (ds != null && ds.Tables.Count > 0)
                int.TryParse(ds.Tables[0].Rows[0][0].ToString(), out total);
            return total;
        }

        /// <summary>
        /// 清除错题库
        /// </summary>
        public int ClearErrorItem()
        {
            string sql = "truncate table ErrorItem";
            return DBUtility.DbHelperSQL.ExecuteSql(sql) > 0 ? 1 : 2;
        }
    }
}
